<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
  
// setTimeout(()=>{
//     console.log('2秒后执行')
// },2000)
//      sleep(20000)

const arr = [{
    id: 2,
    name: '部门B',
    parentId: 0
},
{
    id: 3,
    name: '部门C',
    parentId: 1
},
{
    id: 1,
    name: '部门A',
    parentId: 2
},
{
    id: 4,
    name: '部门D',
    parentId: 1
},
{
    id: 5,
    name: '部门E',
    parentId: 2
},
{
    id: 6,
    name: '部门F',
    parentId: 3
},
{
    id: 7,
    name: '部门G',
    parentId: 2
},
{
    id: 8,
    name: '部门H',
    parentId: 4
}
]


function test() {
    let obj = {}
    arr.forEach(({id,name,parentId}) =>{
        if(parentId === 0) {
            obj.id = id
            obj.name = name
            obj.parentId = parentId
            obj.children = []
            arrayToTree(id,arr,obj.children)
        }
    })
    console.log(1)
    console.log(obj)
}

function arrayToTree(topId,arr,topList) {
    // 递归退出条件

    arr.forEach(({id,name,parentId}) =>{
        if(parentId === topId) {
            let obj = {}
            obj.id = id
            obj.name = name
            obj.parentId = parentId
            topList.push(obj)
            obj.children = []
            arrayToTree(id,arr,obj.children)

            // console.log(obj)
        }
    })
}
// test()

// var Fn = {};
// Fn.method = function(){
//     this.name = 'jack';
//     console.log(this);
//     function test(){
//       console.log(this,this === window);
//     }
//     test();
// }
// Fn.method();//true

// 比如这个函数输入一个1，那么要求函数返回A
// const A = [1,2,3];
// const B = [4,5,6];
// const C = [7,8,9];

// function test1(num) {

//    let isA =  A.some(item =>{return item === num})
//    let isB =  B.some(item =>{return item === num})
//    let isC =  C.some(item =>{return item === num})

//     if(isA) {
//         console.log('A')
//     }else if(isB) {
//         console.log('B')
//     }else if(isC) {
//         console.log('C')
//     }else {
//         console.log('未查找到')
//     }
// }


var name = '123';

var obj = {
	name: '456',
	print: function() {
		function a() {
            console.log(this)
			console.log(this.name);
		}
		a();
	}
}

// obj.print();


// JS八种数据类型   string boolean  number null undefined object BigInt Symbol  
// 块级元素   div p h1-h6 ul ol li 
// 行内元素   span i a strong em b    img   input  
function deepCopy(orign,newData) {

let obj = newData||{}
for(let i in orign) {
    if(typeof orign[i] === 'object') {
      
        obj[i] = orign[i] instanceof Array?[]:{}
        deepCopy(orign[i],obj[i])

    }else {
        obj[i] = orign[i]
    }
}
return obj
}

let obj2 = {
name:'梁鹏',
age:12,
habit:[
    {name:'basketball'},
    {name:'football'}

],
aaa:{
    'hell':1
}
}

// console.log(obj2.habit === deepCopy(obj2).habit)
// console.log(deepCopy(obj2))

const obj3 = {
	fn1: () => console.log(this),
	fn2: function() {console.log(this)}
}

obj3.fn1();
obj3.fn2();

// const x = new obj3.fn1();
const y = new obj3.fn2();


    
</script>

</html>